﻿@using CodeBuilderForVisualStudio.Entity
@inherits RazorEngine.Templating.TemplateBase<DALFileModel>
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AutoMapper;
using SqlSugar;
using @(Model.Namespace).Entity.DataTable;
using @(Model.Namespace).Entity.Base;
using @(Model.Namespace).Entity.Input.@(Model.Name);
using @(Model.Namespace).Entity.Output.@(Model.Name);
using @(Model.Namespace).IDAL;
using @(Model.Namespace).DAL.Base;


namespace @(Model.Namespace).DAL
{
    /// <summary>
    /// @(Model.CnName)数据访问层
    ///</summary>
    public class @(Model.Name)DAL : BaseDAL, I@(Model.Name)DAL
    {

        public IMapper Mapper { get; set; }

        /// <summary>
        /// 获取@(Model.CnName)分页列表
        ///</summary>
        public async Task<PageResult<Get@(Model.Name)PageOutput>> Get@(Model.Name)PageResultAsync(Get@(Model.Name)PageInput input)
        {
            PageResult<Get@(Model.Name)PageOutput> result = new PageResult<Get@(Model.Name)PageOutput>();
            RefAsync<int> totalNumber = 0;
            result.Datas = await DbContext.Queryable<@(Model.Name)>()
                //此处为可能需要过滤的条件
    @foreach (ClassProperty classProperty in Model.ClassPropertys)
    {
        if (classProperty.PropertyType == "string")
        {<text>
                .WhereIF(!string.IsNullOrEmpty(input.@(classProperty.Name)), x => x.@(classProperty.Name).Contains(input.@(classProperty.Name)))</text>
    }
    else
    {
        <text>
                .Where(x => x.@(classProperty.Name) == input.@(classProperty.Name))</text>
    }
    }
    
                .Select(x => new Get@(Model.Name)PageOutput
                {
                    //推荐使用SqlSugar查询方式返回需要的字段
        @foreach (ClassProperty classProperty in Model.ClassPropertys)
{<text>
                    @classProperty.Name = x.@classProperty.Name,//@classProperty.CnName</text>}
                
                })            
                .ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
            result.DataCount = totalNumber.Value;
            return result;
        }

        /// <summary>
        /// 获取@(Model.CnName)分页列表
        ///</summary>
        public async Task<List<Get@(Model.Name)PageOutput>> Get@(Model.Name)PageListAsync(Get@(Model.Name)PageInput input)
        {
            return await DbContext.Queryable<@(Model.Name)>()
                //此处为可能需要过滤的条件
    @foreach (ClassProperty classProperty in Model.ClassPropertys)
    {
        if (classProperty.PropertyType == "string")
        {<text>
                .WhereIF(!string.IsNullOrEmpty(input.@(classProperty.Name)), x => x.@(classProperty.Name).Contains(input.@(classProperty.Name)))</text>
    }
    else
    {
        <text>
                .Where(x => x.@(classProperty.Name) == input.@(classProperty.Name))</text>
    }
    }
    
                .Select(x => new Get@(Model.Name)PageOutput
                {
                    //推荐使用SqlSugar查询方式返回需要的字段
        @foreach (ClassProperty classProperty in Model.ClassPropertys)
{<text>
                    @classProperty.Name = x.@classProperty.Name,//@classProperty.CnName</text>}
                
                })        
                .ToPageListAsync(input.PageIndex, input.PageSize);
        }

        /// <summary>
        /// 获取@(Model.CnName)
        /// </summary>
        public async Task<Get@(Model.Name)Output> Get@(Model.Name)OutputAsync(BaseGuidEntity input)
        {
            return await DbContext.Queryable<@(Model.Name)>()
                .Where(x => x.Id == input.Id)
                .Select(x => new Get@(Model.Name)Output
                {
                    //推荐使用SqlSugar查询方式返回需要的字段
    @foreach (ClassProperty classProperty in Model.ClassPropertys)
{<text>
                    @classProperty.Name = x.@classProperty.Name,//@classProperty.CnName</text>}
                
                })        
                .FirstAsync();
        }

        /// <summary>
        /// 获取全部@(Model.CnName)
        /// </summary>
        public async Task<List<Get@(Model.Name)Output>> GetAll@(Model.Name)ListAsync()
        {
            return await DbContext.Queryable<@(Model.Name)>()
                .Select(x => new Get@(Model.Name)Output
                {
                    //推荐使用SqlSugar查询方式返回需要的字段
    @foreach (ClassProperty classProperty in Model.ClassPropertys)
{<text>
                    @classProperty.Name = x.@classProperty.Name,//@classProperty.CnName</text>}
                
                })        
                .ToListAsync();
        }

        /// <summary>
        /// 修改@(Model.CnName)
        /// </summary>
        public async Task Update@(Model.Name)Async(Update@(Model.Name)Input input)
        {
            @(Model.Name) entity = Mapper.Map<@(Model.Name)>(input);
            await DbContext.Updateable(entity)
                //.IgnoreColumns(x => x.CreatedTime)//排除不需要更新的列
                .ExecuteCommandAsync();
        }

        /// <summary>
        /// 新增@(Model.CnName)
        /// </summary>
        public async Task Add@(Model.Name)Async(Add@(Model.Name)Input input)
        {
            @(Model.Name) entity = Mapper.Map<@(Model.Name)>(input);
            entity.Id = Guid.NewGuid().ToString();
            await DbContext.Insertable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 删除@(Model.CnName)
        /// </summary>
        public async Task Delete@(Model.Name)Async(BaseIdListInput input)
        {
            await DbContext.Deleteable<@(Model.Name)>()
                .Where(x => input.Ids.Contains(x.Id))
                .ExecuteCommandAsync();
        }
    }
}